package top.chmin.project.cloud.common.entity.label.value.auto;

// field import auto
// #bind.field.tableCode#

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.chmin.project.cloud.common.entity.Page;
import top.chmin.project.cloud.common.entity.auto.Entity;
import top.chmin.project.cloud.common.entity.auto.EntityDao;
import top.chmin.project.cloud.common.entity.auto.EntityFPC;
import top.chmin.project.cloud.common.entity.column.auto.EntityColumn;
import top.chmin.project.cloud.common.entity.column.auto.EntityColumnDao;
import top.chmin.project.cloud.common.entity.column.auto.EntityColumnFPC;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * chmin
 * 2023-12-27 15:15:34
 * cloud-builder
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EntityLabelValueService {

    @Resource
    EntityLabelValuePlugin plugin;

    @Resource
    EntityLabelValueDao entityLabelValueDao;

    // field labelDao auto
// #bind.field.tableCode#
    @Resource
    EntityDao entityDao;
// $bind.field.tableCode$

    // #bind.field.valueFieldCode#
    @Resource
    EntityColumnDao entityColumnDao;
// $bind.field.valueFieldCode$


    public int save(EntityLabelValueSave.Param param) {
        plugin.checkSave(param);
        EntityLabelValue entityLabelValue = BeanUtil.toBean(param, EntityLabelValue.class);
        plugin.beforeSave(entityLabelValue);
        int result = entityLabelValueDao.save(entityLabelValue);
        plugin.afterSave(param, entityLabelValue, result);
        return result;
    }

    public int updateById(EntityLabelValueUpdateById.Param param) {
        plugin.checkUpdateById(param);
        EntityLabelValue entityLabelValue = BeanUtil.toBean(param, EntityLabelValue.class);
        plugin.beforeUpdateById(entityLabelValue);
        int result = entityLabelValueDao.updateById(entityLabelValue);
        plugin.afterUpdateById(param, entityLabelValue, result);
        return result;
    }

    public int deleteById(EntityLabelValueDeleteById.Param param) {
        plugin.checkDeleteById(param);
        int result = entityLabelValueDao.deleteById(param.getId());
        plugin.afterDeleteById(param, result);
        return result;
    }

    public EntityLabelValueSelectById.Result selectById(EntityLabelValueSelectById.Param param) {
        plugin.checkSelectById(param);
        EntityLabelValueFPC fpc = new EntityLabelValueFPC().id().eq(param.getId());
        plugin.beforeSelectById(param, fpc);
        EntityLabelValueSelectById.Result result = entityLabelValueDao.getFirst(fpc.toCondition(), EntityLabelValueSelectById.Result.class);
        plugin.afterSelectById(param, result);
        return result;
    }

    public Page<EntityLabelValueSelectPage.Result> selectPage(EntityLabelValueSelectPage.Param param) {
        plugin.checkSelectPage(param);
        EntityLabelValueFPC fpc = new EntityLabelValueFPC();
        fpc = param.isDesc() ? fpc.id().desc() : fpc.id().asc();
        plugin.beforeSelectPage(param, fpc);
        Page<EntityLabelValueSelectPage.Result> result = entityLabelValueDao.getPageByCondition(fpc.toCondition(), EntityLabelValueSelectPage.Result.class);
// field label auto
// #bind.field.tableCode#
        if (CollectionUtil.isNotEmpty(result.getList())) {
            List<String> tableCodeList = result.getList().stream()
                    .map(EntityLabelValueSelectPage.Result::getTableCode)
                    .toList();
            Stream<Entity> tableCodeStream = entityDao.getStreamByCondition(
                    new EntityFPC()
                            .forDistinct()
                            .tableCode().in(tableCodeList)
                            .toCondition()
            );
            Map<String, Entity> tableCodeMap = tableCodeStream.collect(Collectors.toMap(
                    Entity::getTableCode,
                    t -> t
            ));
            result.getList().forEach(r -> {
                Entity entity = tableCodeMap.get(r.getTableCode());
                r.setTableCodeLabel(entity == null ? null : entity.getTableName());
            });
        }
// $bind.field.tableCode$

// #bind.field.labelFieldCode#
        if (CollectionUtil.isNotEmpty(result.getList())) {
            List<String> labelFieldCodeList = result.getList().stream()
                    .map(EntityLabelValueSelectPage.Result::getLabelFieldCode)
                    .toList();
            Stream<EntityColumn> labelFieldCodeStream = entityColumnDao.getStreamByCondition(
                    new EntityColumnFPC()
                            .forDistinct()
                            .fieldCode().in(labelFieldCodeList)
                            .toCondition()
            );
            Map<String, EntityColumn> labelFieldCodeMap = labelFieldCodeStream.collect(Collectors.toMap(
                    EntityColumn::getFieldCode,
                    t -> t
            ));
            result.getList().forEach(r -> {
                EntityColumn entityColumn = labelFieldCodeMap.get(r.getLabelFieldCode());
                r.setLabelFieldCodeLabel(entityColumn == null ? null : entityColumn.getFieldName());
            });
        }
// $bind.field.labelFieldCode$

// #bind.field.valueFieldCode#
        if (CollectionUtil.isNotEmpty(result.getList())) {
            List<String> valueFieldCodeList = result.getList().stream()
                    .map(EntityLabelValueSelectPage.Result::getValueFieldCode)
                    .toList();
            Stream<EntityColumn> valueFieldCodeStream = entityColumnDao.getStreamByCondition(
                    new EntityColumnFPC()
                            .forDistinct()
                            .fieldCode().in(valueFieldCodeList)
                            .toCondition()
            );
            Map<String, EntityColumn> valueFieldCodeMap = valueFieldCodeStream.collect(Collectors.toMap(
                    EntityColumn::getFieldCode,
                    t -> t
            ));
            result.getList().forEach(r -> {
                EntityColumn entityColumn = valueFieldCodeMap.get(r.getValueFieldCode());
                r.setValueFieldCodeLabel(entityColumn == null ? null : entityColumn.getFieldName());
            });
        }
// $bind.field.valueFieldCode$

        plugin.afterSelectPage(param, result);
        return result;
    }

// entity select auto

// entity search auto

}
